import 'package:buy_flutter/page/main/bloc/main_bloc.dart';
import 'package:buy_flutter/page/mine/view/mine_view.dart';
import 'package:buy_flutter/widgets/common_tabbar_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class UnitView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<MainBloc>(
      create: (context) => MainBloc(),
      child: Scaffold(
        bottomNavigationBar: MainBottomView(),
        body: MainContainView(),
      ),
    );
  }
}

class MainContainView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MainBloc, MainState>(
      builder: (context, state) {
        return pages[state.index];
      },
    );
  }

  final pages = [
    Container(
        child: Center(
          child: const Text('index'),
        )),
    Container(
        child: Center(
          child: const Text('course'),
        )),
    Container(
        child: Center(
          child: const Text('college'),
        )),
    Container(
      child: MinePage(),
    )
  ];
}

class MainBottomView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MainBloc, MainState>(
      builder: (context, state) {
        return BottomNavigationBar(
          items: bottomNavItems,
          currentIndex: state.index,
          onTap: (index) {
            context.read<MainBloc>().add(BottomNavigationChanged(index));
          },
          showSelectedLabels: true,
          showUnselectedLabels: true,
          unselectedLabelStyle: TextStyle(color: Colors.black),
          selectedItemColor: Colors.deepOrange,
          unselectedItemColor: Colors.black,
        );
      },
    );
  }

  final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      backgroundColor: Colors.white,
      icon: Image.asset(
        "assets/home/index.png",
        width: 27,
        height: 27,
      ),
      activeIcon: Image.asset(
        "assets/home/index_selected.png",
        width: 27,
        height: 27,
      ),
      label: "优惠",
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.white,
      icon: Image.asset(
        "assets/home/course.png",
        width: 27,
        height: 27,
      ),
      activeIcon: Image.asset(
        "assets/home/course_selected.png",
        width: 27,
        height: 27,
      ),
      label: "课程",
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.white,
      icon: Image.asset(
        "assets/home/college.png",
        width: 27,
        height: 27,
      ),
      activeIcon: Image.asset(
        "assets/home/college_selected.png",
        width: 27,
        height: 27,
      ),
      label: "学习中心",
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.white,
      icon: Image.asset(
        "assets/home/my.png",
        width: 27,
        height: 27,
      ),
      activeIcon: Image.asset(
        "assets/home/my_selected.png",
        width: 27,
        height: 27,
      ),
      label: "我的",
    ),
  ];
}
